﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Video;

public class VIPResourceUtil {

    private static Dictionary<string, Sprite> spriteDic = new Dictionary<string, Sprite>();
    private static Dictionary<string, AudioClip> audioDic = new Dictionary<string, AudioClip>();
    private static Dictionary<string, VideoClip> videoDic = new Dictionary<string, VideoClip>();
    private static Dictionary<string, GameObject> objDic = new Dictionary<string, GameObject>();

    /// <summary>
    /// 精灵
    /// </summary>
    /// <param name="path">Resources文件夹下的文件的名字(如果有子文件,则用/分割)</param>
    /// <returns></returns>
    public static Sprite GetSprite(string path)
    {
        Sprite oldSprite;
        if (!spriteDic.TryGetValue(path, out oldSprite))
        {
            Sprite newSprite = Resources.Load<Sprite>(path);
            spriteDic.Add(path, newSprite);
            return newSprite;
        }
        else
        {
            return oldSprite;
        }
    }

    /// <summary>
    /// 声音片段
    /// </summary>
    /// <param name="path"></param>
    /// <returns></returns>
    public static AudioClip GetAudio(string path)
    {
        AudioClip oldAudio;
        if (!audioDic.TryGetValue(path, out oldAudio))
        {

            AudioClip newAudio = Resources.Load<AudioClip>(path);
            audioDic.Add(path, newAudio);
            return newAudio;
        }
        else
        {
            return oldAudio;
        }
    }

    /// <summary>
    /// 视频片段
    /// </summary>
    /// <param name="path"></param>
    /// <returns></returns>
    public static VideoClip GetVideo(string path)
    {
        VideoClip oldVideo;
        if (!videoDic.TryGetValue(path, out oldVideo))
        {

            VideoClip newVideo = Resources.Load<VideoClip>(path);
            videoDic.Add(path, newVideo);
            return newVideo;
        }
        else
        {
            return oldVideo;
        }
    }

    /// <summary>
    /// 对象
    /// </summary>
    /// <param name="path"></param>
    /// <returns></returns>
    public static GameObject GetObj(string path)
    {
        GameObject oldObj;
        if (!objDic.TryGetValue(path, out oldObj))
        {
            GameObject newObj = Resources.Load<GameObject>(path);
            objDic.Add(path, newObj);
            return newObj;
        }
        else
        {
            return oldObj;
        }
    }

}
